//
//  BSSEnterpriseCertificationBillAddTwoPhotoNameCellTableViewCell.m
//  PreschoolEducation
//
//  Created by 李传铎 on 2020/11/13.
//  Copyright © 2020 李传铎. All rights reserved.
//

#import "BSSEnterpriseCertificationBillAddTwoPhotoNameCellTableViewCell.h"
#import "UIButton+WebCache.h"
#import "BSSUploadImagApi.h"

@interface BSSEnterpriseCertificationBillAddTwoPhotoNameCellTableViewCell ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (nonatomic, strong) UIImagePickerController               *imagePicker;
@property (nonatomic, assign) BOOL               isNext;
@end
@implementation BSSEnterpriseCertificationBillAddTwoPhotoNameCellTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor clearColor];
        [self createUI];
        [self layoutUI];
    }
    return self;
}

- (void)createUI {
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.idNoPositiveBtn];
    [self.contentView addSubview:self.idNoPositiveDeleteBtn];
    [self.contentView addSubview:self.idNoPositiveLab];
    [self.contentView addSubview:self.idNoReverseBtn];
    [self.contentView addSubview:self.idNoReverseDeleteBtn];
    [self.contentView addSubview:self.idNoReverseLab];
    [self.contentView addSubview:self.line];
}

- (void)layoutUI {
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(__kSize(22));
    }];

    float width = (SCREEN_WIDTH - 30 - 10)/2;
    [_idNoPositiveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.size.mas_equalTo(__kCGSizeMake(width, 105));
        make.top.mas_equalTo(_titleLab.mas_bottom).offset(12);
    
    }];
    _idNoPositiveBtn.tag = 1000;
    _idNoPositiveBtn.backgroundColor = [UIColor blueColor];
    [_idNoPositiveBtn addTarget:self action:@selector(chooseImge:) forControlEvents:UIControlEventTouchUpInside];
    [_idNoPositiveDeleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(__kSize(16), __kSize(16)));
        make.right.top.mas_equalTo(_idNoPositiveBtn);
        
    }];
    
    [_idNoPositiveLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_idNoPositiveBtn.mas_centerX);
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(_idNoPositiveBtn.mas_bottom).mas_offset(__kSize(12));
        make.width.mas_equalTo(width);

    }];
    
    [_idNoReverseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_idNoPositiveBtn.mas_right).offset(10);
        make.size.mas_equalTo(__kCGSizeMake(width, 105));
        make.top.mas_equalTo(_titleLab.mas_bottom).offset(12);
    
    }];
    _idNoReverseBtn.backgroundColor = [UIColor blueColor];
    [_idNoReverseBtn addTarget:self action:@selector(chooseImge:) forControlEvents:UIControlEventTouchUpInside];
    _idNoReverseBtn.tag = 1001;
    [_idNoReverseDeleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(__kSize(16), __kSize(16)));
        make.right.top.mas_equalTo(_idNoReverseBtn);
        
    }];
    
    [_idNoReverseLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_idNoReverseBtn.mas_centerX);
        make.top.mas_equalTo(_idNoReverseBtn.mas_bottom).mas_offset(__kSize(12));
        make.width.mas_equalTo(width);

    }];
    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_idNoPositiveLab.mas_bottom).mas_offset(__kSize(12));
        make.left.mas_equalTo(__kSize(22));
        make.right.mas_equalTo(__kSize(- 22));
        make.height.mas_equalTo(0.5);
        make.bottom.mas_equalTo(self.contentView);
    }];
}
- (void)setModel:(BSSEnterpriseCertificationModel *)model{
    _model = model;
    [self.idNoPositiveBtn sd_setImageWithURL:[NSURL URLWithString:Format(model.value)] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:Format(model.placeholderImage)]];
    [self.idNoReverseBtn sd_setImageWithURL:[NSURL URLWithString:Format(model.nextValue)] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:Format(model.placeholderImage)]];
    self.idNoPositiveLab.text = model.name;
    self.idNoReverseLab.text = model.nextName;
  
}
- (void)chooseImge:(UIButton *)sender {
    if (sender.tag == 1001) {
        _isNext = YES;
    }
    UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [[self getCurrentVC] presentViewController:self.imagePicker animated:YES completion:nil];
        }
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"从相册获取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [[self getCurrentVC] presentViewController:self.imagePicker animated:YES completion:nil];
        }
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [actionSheetController addAction:cancelAction];
    [actionSheetController addAction:action1];
    [actionSheetController addAction:action2];
    
    [[self getCurrentVC] presentViewController:actionSheetController animated:YES completion:nil];
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
   
    [self uploadImage:image];
}
- (void)uploadImage:(UIImage *)image {
    [MBProgressHUD showHUD];
    BSSUploadImagApi *api  = [[BSSUploadImagApi alloc] initWithURL:@"" dic:@{} imageArray:@[image]];
    api.isFiles = NO;
    [api startRequest:^(BSSRequestStatus requestStatus, NSString *message, id responseObject) {
        
        [MBProgressHUD hideHUD];
        if (requestStatus == BSSRequestStatusSuccess) {
            
            if (!IsEmpty(responseObject[@"data"])) {
                NSDictionary *dataDic = [responseObject valueForKey:@"data"];
                NSString *imageURL = [dataDic valueForKey:@"imageURL"];
                if (self.isNext) {
                    self.model.nextValue = imageURL;
                    self.idNoReverseDeleteBtn.hidden = NO;
                    [self.idNoReverseBtn sd_setImageWithURL:[NSURL URLWithString:Format(self.model.nextValue)] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:Format(self.model.placeholderImage)]];
                }else{
                    self.model.value = imageURL;
                    self.idNoPositiveDeleteBtn.hidden = NO;
                    [self.idNoPositiveBtn sd_setImageWithURL:[NSURL URLWithString:Format(self.model.value)] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:Format(self.model.placeholderImage)]];
                }
                
            }
            
        }else{
            [MBProgressHUD showText:message];
        }
    }];

}
#pragma mark - action
- (void)idNoReverseDeleteBtnClick:(UIButton *)sender {
    if (IsEmpty(self.model.deleUrls)) {
        self.model.nextDeleUrls = self.model.nextValue;
    }else{
        self.model.nextDeleUrls = [NSString stringWithFormat:@"%@,%@",self.model.nextDeleUrls,self.model.nextValue];
    }
    
    self.model.nextValue = @"";
    [self.idNoReverseBtn sd_setImageWithURL:[NSURL URLWithString:Format(self.model.nextValue)] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:Format(self.model.placeholderImage)]];
    sender.hidden = YES;
}
- (void)idNoPositiveDeleteBtnClick:(UIButton *)sender {
    if (IsEmpty(self.model.deleUrls)) {
        self.model.deleUrls = self.model.value;
    }else{
        self.model.deleUrls = [NSString stringWithFormat:@"%@,%@",self.model.deleUrls,self.model.value];
    }
    
    self.model.value = @"";
    [self.idNoPositiveBtn sd_setImageWithURL:[NSURL URLWithString:Format(self.model.value)] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:Format(self.model.placeholderImage)]];
    sender.hidden = YES;
}
#pragma mark - setter & getter
- (UILabel *)titleLab{
    if (!_titleLab){
        _titleLab = [UILabel new];
        _titleLab.textColor = __kColor(51, 51, 51);
        _titleLab.font = fontCount(14);
        
        NSString *title = @"*请上传营业执照";
        
        NSRange range = [title rangeOfString:@"*"];
        NSMutableAttributedString *outStr = [[NSMutableAttributedString alloc] initWithString:title];
        [outStr addAttributes:@{NSForegroundColorAttributeName : __kColor(193, 49, 54)} range:range];
        _titleLab.attributedText = outStr;
    }
    return _titleLab;
}
- (UIButton *)idNoPositiveBtn{
    if (!_idNoPositiveBtn){
        _idNoPositiveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_idNoPositiveBtn setBackgroundImage:[UIImage imageNamed:@"market_enterprise_up"] forState:UIControlStateNormal];
    }
    return _idNoPositiveBtn;
}
- (UIButton *)idNoPositiveDeleteBtn{
    if (!_idNoPositiveDeleteBtn) {
        _idNoPositiveDeleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_idNoPositiveDeleteBtn setImage:[UIImage imageNamed:@"market_delete"] forState:UIControlStateNormal];
        [_idNoPositiveDeleteBtn addTarget:self action:@selector(idNoPositiveDeleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _idNoPositiveDeleteBtn.hidden = YES;
    }
    return _idNoPositiveDeleteBtn;
}
- (UILabel *)idNoPositiveLab{
    if (!_idNoPositiveLab){
        _idNoPositiveLab = [UILabel new];
        _idNoPositiveLab.font = __kFont(13);
        _idNoPositiveLab.textColor = __kColor(153, 153, 153);
        _idNoPositiveLab.text = @"营业执照照片";
        _idNoPositiveLab.textAlignment = NSTextAlignmentCenter;
    }
    return _idNoPositiveLab;
}

- (UIButton *)idNoReverseBtn{
    if (!_idNoReverseBtn){
        _idNoReverseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_idNoReverseBtn setBackgroundImage:[UIImage imageNamed:@"market_enterprise_up"] forState:UIControlStateNormal];
    }
    return _idNoReverseBtn;
}
- (UIButton *)idNoReverseDeleteBtn{
    if (!_idNoReverseDeleteBtn) {
        _idNoReverseDeleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_idNoReverseDeleteBtn setImage:[UIImage imageNamed:@"market_delete"] forState:UIControlStateNormal];
        [_idNoReverseDeleteBtn addTarget:self action:@selector(idNoPositiveDeleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _idNoReverseDeleteBtn.hidden = YES;
    }
    return _idNoReverseDeleteBtn;
}
- (UILabel *)idNoReverseLab{
    if (!_idNoReverseLab){
        _idNoReverseLab = [UILabel new];
        _idNoReverseLab.textAlignment = NSTextAlignmentCenter;
        _idNoReverseLab.font = __kFont(13);
        _idNoReverseLab.textColor = __kColor(153, 153, 153);
        _idNoReverseLab.text = @"营业执照照片";
    }
    return _idNoReverseLab;
}
- (UIView *)line{
    if (!_line){
        _line = [UIView new];
        _line.backgroundColor = __kColor(238, 238, 238);
    }
    return _line;
}
- (UIImagePickerController *)imagePicker{
    if (!_imagePicker) {
        _imagePicker = [[UIImagePickerController alloc]init];
        _imagePicker.delegate = self;
        _imagePicker.allowsEditing = YES;
        _imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
    }
    return _imagePicker;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
//获取当前控制器
- (UIViewController *)getCurrentVC{
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
    return currentVC;
    
}

- (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC{
    UIViewController *currentVC;
    
    if ([rootVC presentedViewController]) {
        // 视图是被presented出来的
        rootVC = [rootVC presentedViewController];
    }
    if ([rootVC isKindOfClass:[UITabBarController class]]) {
        // 根视图为UITabBarController
        currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
        
    } else if ([rootVC isKindOfClass:[UINavigationController class]]){
        // 根视图为UINavigationController
        currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
        
    } else {
        // 根视图为非导航类
        currentVC = rootVC;
    }
    return currentVC;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
